package com.ruoyi.service.impl;

import java.util.List;

import com.ruoyi.domain.AdvUpgrade;
import com.ruoyi.domain.DeviceRestart;
import com.ruoyi.mapper.AdvProductsMapper;
import com.ruoyi.mapper.AdvUpgradeMapper;
import com.ruoyi.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.AdvInoutstockMapper;
import com.ruoyi.domain.AdvInoutstock;
import com.ruoyi.service.IAdvInoutstockService;
import org.springframework.transaction.annotation.Transactional;

/**
 * Service业务层处理
 *
 * @author ruoyi
 * @date 2024-05-13
 */
@Service
public class AdvInoutstockServiceImpl implements IAdvInoutstockService
{
    @Autowired
    private AdvInoutstockMapper advInoutstockMapper;

    @Autowired
    private AdvProductsMapper advProductsMapper;
    @Autowired
    private AdvUpgradeMapper advUpgradeMapper;

    /**
     * 查询
     *
     * @param advInoutstock 主键
     * @return
     */
    @Override
    public AdvInoutstock selectAdvInoutstockByAdvInoutstock(Long advInoutstock)
    {
        return advInoutstockMapper.selectAdvInoutstockByAdvInoutstock(advInoutstock);
    }

    /**
     * 查询列表
     *
     * @param advInoutstock
     * @return
     */
    @Override
    public List<AdvInoutstock> selectAdvInoutstockList(AdvInoutstock advInoutstock)
    {
        return advInoutstockMapper.selectAdvInoutstockList(advInoutstock);
    }


    /**
     * 新增
     *
     * @param advInoutstock
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAdvInoutstock(AdvInoutstock advInoutstock)
    {
        List<AdvInoutstock> advInoutstocks = advInoutstockMapper.queryByDNO(advInoutstock.getDevicesno());
        if (advInoutstocks.isEmpty()){
            int stocknum = advProductsMapper.queryProductBefore(advInoutstock.getAdvProductsId());
            advProductsMapper.addStock(stocknum,advInoutstock.getAdvProductsId());
            advInoutstock.setAdvInoutstock(UUIDUtils.timeMillisId());
            return advInoutstockMapper.insertAdvInoutstock(advInoutstock);
        }else {
            throw new RuntimeException("该设备编号已被入库");
        }
    }

    /**
     * 修改
     *
     * @param advInoutstock
     * @return 结果
     */
    @Override
    public int updateAdvInoutstock(AdvInoutstock advInoutstock)
    {
        List<AdvInoutstock> advInoutstocks = advInoutstockMapper.queryByDNO(advInoutstock.getDevicesno());
        if (advInoutstocks.isEmpty()){
            return advInoutstockMapper.updateAdvInoutstock(advInoutstock);
        }else {
            if (advInoutstocks.get(0).getDevicesno().equals(advInoutstock.getDevicesno())){
                return advInoutstockMapper.updateAdvInoutstock(advInoutstock);
            }
            throw new RuntimeException("该设备编号已被入库");
        }
    }

    /**
     * 批量删除
     *
     * @param advInoutstocks 需要删除的主键
     * @return 结果
     */
    @Override
    public int deleteAdvInoutstockByAdvInoutstocks(Long[] advInoutstocks)
    {
        return advInoutstockMapper.deleteAdvInoutstockByAdvInoutstocks(advInoutstocks);
    }

    /**
     * 删除信息
     *
     * @param advInoutstock 主键
     * @return 结果
     */
    @Override
    public int deleteAdvInoutstockByAdvInoutstock(Long advInoutstock)
    {
        return advInoutstockMapper.deleteAdvInoutstockByAdvInoutstock(advInoutstock);
    }

    @Override
    public List<AdvInoutstock> getDeviceno(Long advMembersId) {
        List<AdvInoutstock> devicenos = advInoutstockMapper.getDeviceno(advMembersId);
        return devicenos;
    }

    /**
     * 修改设备重启时间
     * @param advInoutstock
     * @return
     */
    @Override
    public int updateByRestartTime(AdvInoutstock advInoutstock) {
        System.out.println("我的数据----->"+advInoutstock);
        return advInoutstockMapper.updateByRestartTime(advInoutstock);
    }

    /**
     * 批量修改设备重启时间
     * @param advInoutstock
     * @param reboottime
     * @return
     */
    @Override
    public int batchUpdateReboottime(List<Long> advInoutstock, String reboottime) {
        return advInoutstockMapper.batchUpdateReboottime(advInoutstock, reboottime);
    }

    @Override
    public int batchUpdateUpgrade(DeviceRestart deviceRestart) {
        return advInoutstockMapper.batchUpdateUpgrade(deviceRestart.getIds(), deviceRestart.getUpdatetime(),deviceRestart.getAdvUpgradeId());
    }

    @Override
    public boolean syncSetting(AdvInoutstock advInoutstock) {
        int result = advInoutstockMapper.syncSetting(advInoutstock);
        return result != 0;
    }
}
